var SearchVideoMap = new Object();
var gSearchBoxObj = null;
var SearchVideo = function(opt){
	this.target = opt.target?opt.target:'searchBox';
	this.content = opt.content?opt.content:'pSearchWin';
	this.searchType = opt.searchType?opt.searchType:'movies';
	this.showPage = opt.showPage===false?false:true;
	this.pageSize = opt.pageSize?opt.pageSize:20;
	this.pageBox = 'searchWinPageBox';
	this.searchBox = 'pSearchWinTitle';
	this.keyBox = 'pSearchKeyBox';
	this.searchText = 'pSearchText';
	this.hasInit = false;
	
	this.keyCurClass = '_searchKeyCurItem';
	this.keyDarkClass = '_searchKeyDarkItem';
	this.keyClass = '_searchKeyItem';
	this.delCurClass = '_searchKeyCurDel';
	this.delDarkClass = '_searchKeyDarkDel';
	this.delClass = '_searchKeyDel';
	this.text = '';
	this.curPage = 0;
	this.totalPage = 0;
	this.curRegion= 'key';
	this.lastFocusA = false;
	if(opt.formatHtml){
		this.formatHtml = opt.formatHtml;
	}
	this.changeText = opt.changeText;
	this.apptentDataListInterface = opt.apptentDataListInterface;
	this.onHide = opt.onHide;
	this.onclick = opt.onclick;
	
	this.mTextList = null;
	gSearchBoxObj = this;
}

SearchVideo.prototype = {
	setPageInfo:function(p,t){
		this.curPage = p;
		this.totalPage = t;
	},
	setTotalPage:function(page){
		this.totalPage = page;
	},
	setCurPage:function(page){
		this.curPage = page;
	},
	hide:function(){
		this.lastFocusA = document.activeElement; alert(this.lastFocusA.innerHTML);
		KEY.CURTYPE = false;
		this.onHide();
		var content = document.getElementById(this.content);
		content.style.display = 'none';
	},
	show:function(){
		if(!this.hasInit){
			this.init();
		}
		var content = document.getElementById(this.content);
		content.style.display = 'block';
		if(this.lastFocusA ){
			this.lastFocusA.focus();
		}else{
			var keyBox = document.getElementById(this.keyBox);
			var aList = keyBox.getElementsByTagName('a');
			aList[0].focus();
		}
	},
	init:function(){
		var searchBox = document.getElementById(this.searchBox );
		searchBox.innerHTML = '<div class="_searchWin_'+this.searchType+'">&nbsp;</div>';
		var keyBox = document.getElementById(this.keyBox);
		var aList = keyBox.getElementsByTagName('a');
		var a;
		for(var i=0;i<aList.length;i++){
			a = aList[i];
			
			a.onfocus = this.focusKey;
			a.onblur = this.blurKey;
			a.onclick = this.clickKey;
		}
		
		this.mTextList = new TextList({
			target:'searchTextList',
			content:'pSearchTextListBox',
			splitBar:'pSearchTextListSplitBar',
			splitHeight:'553',
			focusClass:'_sTextListCurItem',
			normalClass:'_sTextListItem',
			emptyClass:'_sTextListItem0',
			tipClass:'_sTextListTip',
			onfocus:this.onfocusList,
			formatHtml:this.formatHtml,
			disNum:7,
			onclick:this.onclick,
			tip:lang.tip.searchTip,
			
		});
	},
	onfocusList:function(data,tar){
		gSearchBoxObj.chargeRegion("list");
		KEY.CURTYPE = 'searchBox';
		
		if(gSearchBoxObj.showPage){
			var p = parseInt((tar - 1)/gSearchBoxObj.pageSize) + 1;
			gSearchBoxObj.changePage(p);
		}
	},
	focusKey:function(event){
		var a = event.currentTarget;
		var par = a.parentNode;
		var tar = a.getAttribute('tar');
		gSearchBoxObj.chargeRegion("key");
		if(tar == 'del'){
			par.className = gSearchBoxObj.delCurClass;
		}else{
			par.className = gSearchBoxObj.keyCurClass;
		}
		KEY.CURTYPE = 'searchBox';
		
	},
	chargeRegion:function(r){
		if(this.curRegion != r){
			
				var keyBox = document.getElementById(this.keyBox);
				var aList = keyBox.getElementsByTagName('a');
				var a;
				for(var i=0;i<aList.length;i++){
					a = aList[i];
					var tar = a.getAttribute('tar');
					var par = a.parentNode;
					if(tar == 'del'){
						if(this.curRegion == 'key'){
							par.className = gSearchBoxObj.delDarkClass;
						}else{
							par.className = gSearchBoxObj.delClass;
						}
					}else{
						if(this.curRegion == 'key'){
							par.className = gSearchBoxObj.keyDarkClass;
						}else{
							par.className = gSearchBoxObj.keyClass;
						}
					}
				}
			
			this.curRegion = r;
		}
	},
	blurKey:function(event){
		var a = event.currentTarget;
		var par = a.parentNode;
		var tar = a.getAttribute('tar');
		if(tar == 'del'){
			par.className = gSearchBoxObj.delClass;
		}else{
			par.className = gSearchBoxObj.keyClass;
		}
	},
	clickKey:function(event){
		var a = event.currentTarget;
		var par = a.parentNode;
		var tar = a.getAttribute('tar');
		if(tar == 'del'){
			gSearchBoxObj.delText();
		}else{
			gSearchBoxObj.addText(tar);
		}
	},
	delText:function(){
		if(this.text.length > 0){
			this.text = this.text.substring(0,this.text.length - 1);
			this.changeTextFun();
		}
	},
	addText:function(v){
		this.text+=v;
		
		this.changeTextFun();
	},
	changeTextFun:function(){
		var textBox = document.getElementById(this.searchText);
		textBox.innerHTML = this.text;
		document.refresh();
		this.mTextList.clear();
		this.changeText(this.text);
	},
	formatHtml:function(data,id){
		var con = '';
		
		con += '<div class="_sTextListItemTitle"><a href="#" id="'+this.target+'-a-'+id+'">'+data.name+'</a></div>';
		con +='<div class="_sTextListItemText">'+data.name+'</div>';
		return con;
	},
	setDataList:function(dataList){
		this.mTextList.setDataList(dataList);
		if(this.showPage){
			this.changePage(1);
		}
	},
	changePage :function (p){
		var pageBox = document.getElementById(this.pageBox);
		pageBox.innerHTML = 'Page:'+p+'/'+this.totalPage;
	},appendDataList:function(dataList){
		this.mTextList.appendDataList(dataList);
	}
}


KEY.TYPE['searchBox'] = keyRecvSearchBox;

function keyRecvSearchBox(key){
	if(key == 0x11){		//返回
		gSearchBoxObj.hide();
		return false;
	}else if(key == 0xB && gSearchBoxObj.curRegion == 'list'){		//向下
		if(gSearchBoxObj.showPage && gSearchBoxObj.curPage < gSearchBoxObj.totalPage){
			if(gSearchBoxObj.mTextList.isLast()){		//如果在最小一行
				if(KEY.PRESS){
					gSearchBoxObj.apptentDataListInterface(gSearchBoxObj.text,gSearchBoxObj.curPage+1);
				}
				return false;
			}
		}
	}
	return true;
}